﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.DataContract;
using MaxLeaf.Platform.DALImplementation;
using CommonsLib.DB;
using MaxLeaf.Platform.Functions;
using System.Data.SqlClient;

namespace MaxLeaf.Platform.BRL
{
    public class SCPCategoryBRL : BaseBRL
    {

        public override bool AddValid(DBAccess dbcon, BaseDAL dal, object model)
        {
            if (!base.AddValid(dbcon, dal, model))
            {
                return false;
            }

            if (!CheckParentSeqNo(dbcon, dal, model))
            {
                return false;
            }
            if (!CheckSCPCategoryName(dbcon, dal, model))
            {
                return false;
            }

            return true;
        }

        public override bool UpdateValid(DBAccess dbcon, BaseDAL dal, object model)
        {
            if (!base.UpdateValid(dbcon, dal, model))
            {
                return false;
            }

            if (!CheckParentSeqNo(dbcon, dal, model))
            {
                return false;
            }
            if (!CheckSCPCategoryName(dbcon, dal, model))
            {
                return false;
            }
            return true;
        }

        private bool CheckParentSeqNo(DBAccess dbcon, BaseDAL dal, object model)
        {
            SCPCategoryDataModel scpcategory = (SCPCategoryDataModel)model;
            
            if (scpcategory.ParentSCPCategorySeqNo.Equals(0)) return true;

            string strSql = "select top 1 * from SCPCategory  where FinterID<>@FinterID and CompanyID=@CompanyID and SCPCategorySeqNo=@SCPCategorySeqNo";
            using(System.Data.Common.DbDataReader dr=dbcon.GetDataFirstRow(strSql,new SqlParameter[]
                {
                    new SqlParameter("@FinterID",scpcategory.Key)
                    ,new SqlParameter("@CompanyID",scpcategory.CompanyID)
                    ,new SqlParameter("@SCPCategorySeqNo",scpcategory.ParentSCPCategorySeqNo)
                }) )
            {
                if (dr.Read())
                {                    
                    return true;
                }
            }
            throw new MaxLeaf.Platform.Commons.MaxLeafException(string.Format("无效 ParentSCPCategorySeqNo {0}!", scpcategory.ParentSCPCategorySeqNo));
        }

        private bool CheckSCPCategoryName(DBAccess dbcon, BaseDAL dal, object model)
        {
            SCPCategoryDataModel scpcategory = (SCPCategoryDataModel)model;

            string strSql = @"select top 1 * from SCPCategory  
                where FinterID<>@FinterID 
                and CompanyID=@CompanyID 
                and ParentSCPCategorySeqNo=@ParentSCPCategorySeqNo 
                and SCPCategoryName=@SCPCategoryName";
            using (System.Data.Common.DbDataReader dr = dbcon.GetDataFirstRow(strSql, new SqlParameter[]
                {
                    new SqlParameter("@FinterID",scpcategory.Key)
                    ,new SqlParameter("@CompanyID",scpcategory.CompanyID)
                    ,new SqlParameter("@ParentSCPCategorySeqNo",scpcategory.ParentSCPCategorySeqNo)
                    ,new SqlParameter("@SCPCategoryName",scpcategory.Name)
                }))
            {
                if (!dr.Read())
                {
                    return true;
                }
            }
            throw new MaxLeaf.Platform.Commons.MaxLeafException(string.Format("名称 {0} 已存在!", scpcategory.Name));
        }
         
    }
}
